package com.junmeng.jdemo.recycler.chat.viewstub;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.recycler.decoration.BaseLayoutOrderItemDecoration;
import com.junmeng.jdemo.R;

public class ChatTimeItemDecoration2 extends BaseLayoutOrderItemDecoration {
    private TextView timeView;

    @Override
    public int getItemDecorationLayoutResId() {
        return R.layout.item_chat_time_2;
    }

    @Override
    public void initItemDecorationLayoutViews(@NonNull View itemView) {
        timeView = itemView.findViewById(R.id.tv_text);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        int count = parent.getItemDecorationCount();
        Log.i("123456", "ItemDecorationCount=" + count);
        if (position % 3 == 0) {
            timeView.setText("北京时间：" + position);
            outRect.top = getItemDecorationHeight();
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        Rect rect = new Rect();
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(child);
            if (position % 3 == 0) {


                timeView.setText("北京时间：" + position);
                notifyItemDecorationChanged();
                //            setCurrentItemDecorationRestMeasureWidth(getRestMeasureWidth(child));
                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
                int left = child.getLeft() - lp.leftMargin;
                int top = child.getTop() - lp.topMargin - getItemDecorationHeight();
                int right = child.getRight() + lp.rightMargin;
                int bottom = top + getItemDecorationHeight();

                c.save();

//                Rect r = getCurrentDrawTopRectPosition(child, parent, state);
                Rect r = getCurrentOrderDecoratedBoundsWithMargins(child, parent, state);
                left = r.left;
                top = r.top;
                c.translate(left, top);
                mItemView.draw(c);
                c.restore();
            }

        }
    }
}
